#include "professiondialog.h"
#include "ui_professiondialog.h"

#include "abilitylistmodel.h"

ProfessionDialog::ProfessionDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::ProfessionDialog),  newMode(false), profalm(0), alm(0)
{
    ui->setupUi(this);

    connect(ui->cbProfession, SIGNAL(activated(QString)), SLOT(setProfession(QString)));
    connect(ui->cbAbilities->lineEdit(), SIGNAL(returnPressed()), SLOT(on_pbAddAbil_clicked()));
    connect(ui->cbAbilities->lineEdit(), SIGNAL(returnPressed()), ui->cbAbilities->lineEdit(), SLOT(selectAll()));
}

ProfessionDialog::~ProfessionDialog()
{
    delete ui;
}

void ProfessionDialog::setGdrData(QVariantMap* data)
{
    gdrData = data;

    QVariantList occupations = gdrData->value("occupations").toList();
    foreach(QVariant qv, occupations)
        ui->cbProfession->addItem(qv.toMap().value("name").toString());

    setupAbilities();        

    if ( newMode == false )
        setProfession(ui->cbProfession->currentText());
}

void ProfessionDialog::prepareForNew()
{
    ui->frSelect->setVisible(false);
    ui->leProfName->setText("");
    ui->leProfName->setPlaceholderText(tr("Enter Occupation Name"));
    ui->leProfName->setEnabled(true);

    newMode = true;

    delete profalm;
    profalm = new AbilityListModel(this);
    ui->lvAbilities->setModel(profalm);
}

void ProfessionDialog::setProfession(QString name)
{
    ui->leProfName->setText(name);
    ui->leProfName->setEnabled(false);

    delete profalm;
    profalm = new AbilityListModel(this);

    QVariantList occupations = gdrData->value("occupations").toList();
    for(int i = 0; i < occupations.count(); ++i)
    {
        QVariantMap ocp = occupations.at(i).toMap();
        if ( ocp["name"] == name )
        {
            ui->txProfBenefits->setPlainText(ocp.value("benefits").toString());
            QVariantList occupAbilities = ocp["abilities"].toList();

            foreach(QVariant qv, occupAbilities)
                profalm->addAbility(qv.toString(), "");                       
        }
    }

    ui->lvAbilities->setModel(profalm);
}

bool nameLessThan(const QVariant &s1, const QVariant &s2)
{
     return s1.toMap()["name"].toString() < s2.toMap()["name"].toString();
}

void ProfessionDialog::saveProfession()
{
    QVariantMap ocp;
    ocp["name"] = ui->leProfName->text();
    ocp["benefits"] = ui->txProfBenefits->toPlainText();
    QVariantList abils;
    for(int i = 0; i < profalm->rowCount(); ++i)
        abils << profalm->getAbility(i);

    ocp["abilities"] = abils;

    QVariantList occupations = gdrData->value("occupations").toList();
    for(int i = 0; i < occupations.count(); ++i)
    {
        QVariantMap tmp = occupations.at(i).toMap();
        if ( tmp["name"] == ocp["name"] )
        {
            occupations.removeAt(i);
            break;
        }
    }

    occupations.append(ocp);
    qSort(occupations.begin(), occupations.end(), nameLessThan);
    gdrData->remove("occupations");
    gdrData->insert("occupations", occupations);
}

void ProfessionDialog::setupAbilities()
{
    delete alm;
    alm = new AbilityListModel(this);

    QVariantMap abCategs = gdrData->value("abilities").toMap();
    foreach(QString key, abCategs.keys())
    {
        QVariantMap subMap = abCategs.value(key).toMap();
        if ( subMap.empty() )
        {
            setupAbilities(key, QString(), abCategs.value(key).toList());
        }
        else
        {
            foreach(QString subkey, subMap.keys())
            {
                setupAbilities(key, subkey, subMap.value(subkey).toList());
            }
        }
    }

    ui->cbAbilities->setModel(alm);
}

void ProfessionDialog::setupAbilities(QString categ, QString subcateg, QVariantList list)
{
    alm->addAbility(QString(), categ, subcateg, "<i><span style=\"color:gray\">%1</span></i>");
    foreach(QVariant qv, list)
        alm->addAbility(qv.toString(), categ, subcateg);
}

void ProfessionDialog::on_buttons_clicked(QAbstractButton* button)
{
    QDialogButtonBox::StandardButton sb = ui->buttons->standardButton(button);
    if ( sb == QDialogButtonBox::Apply || sb == QDialogButtonBox::Save )
        saveProfession();
    else
        reject();
}

void ProfessionDialog::on_pbAddAbil_clicked()
{
    QString abil = ui->cbAbilities->currentText();
    profalm->addAbility(abil, "");
}

void ProfessionDialog::on_pbRemoveAbil_clicked()
{
    QModelIndexList lst = ui->lvAbilities->selectionModel()->selectedIndexes();
    while ( lst.count() > 0 )
    {
        profalm->removeAt(lst.at(0));
        lst.removeAt(0);
    }
}
